package com.asia5b.a5mall.order.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Observer
import com.asia5b.a5mall.middleware.base.SimpleDialogFragment
import com.asia5b.a5mall.middleware.config.PageTransformerConfig
import com.asia5b.a5mall.middleware.logic.viewmodel.order.LogisDFVM
import com.asia5b.a5mall.middleware.model.order.GuoneiBean
import com.asia5b.a5mall.middleware.model.order.LogisItemBean
import com.asia5b.a5mall.middleware.model.order.OrderDetailModel
import com.asia5b.a5mall.middleware.base.BaseFragmentPagerAdapter
import com.asia5b.a5mall.middleware.utils.DisplayUtils
import com.asia5b.a5mall.middleware.utils.sNewStanceFragment
import com.asia5b.a5mall.middleware.widget.StackCardPageTransformer
import com.asia5b.a5mall.order.databinding.DfLogisBinding
import kotlinx.android.synthetic.main.df_logis.*

/**
 * Created by zhangyuncai on 2020/5/25.
 * 查看物流
 */
class LogisDF : SimpleDialogFragment<LogisDFVM, DfLogisBinding>() {
    override val viewModelClazz: Class<LogisDFVM>
        get() = LogisDFVM::class.java

    override fun getDataBinding(container: ViewGroup?): DfLogisBinding {
        return DfLogisBinding.inflate(layoutInflater,container,false)
    }
    lateinit var orderDetailModel: OrderDetailModel
    val fragmentDatas = arrayListOf<GuoneiBean>()

    val mViewPager by lazy {
        viewpager_logistic
    }

    override fun initView() {
    }

    var tag = 0
    fun notifyViewPager() {
        // 初始化viewpager
        mViewPager.setPageTransformer(true, StackCardPageTransformer.getBuild()
                .setViewType(PageTransformerConfig.RIGHT) //层叠方向
                .setTranslationOffset(DisplayUtils.dip2px(context, 32f).toFloat()) //左右位置偏移量
                .setScaleOffset(DisplayUtils.dip2px(context, 50f).toFloat()) //缩放偏移量
                .setAlphaOffset(1f) //卡片透明度偏移量
                .setMaxShowPage(2) //最大显示的页数
                .create(mViewPager))

        val fragments = arrayListOf<LogisticInformationFragment>()
        for (fragmentData in fragmentDatas) {
            val bundle = Bundle()
            bundle.putSerializable("data", fragmentData)
            bundle.putSerializable("OrderDetailModel", orderDetailModel)
            val fragment = sNewStanceFragment(mActivity, LogisticInformationFragment::class.java, bundle)
            fragments.add(fragment)
        }
        val fragmentAdapter = BaseFragmentPagerAdapter(childFragmentManager, fragments as List<Fragment>)
        mViewPager.adapter = fragmentAdapter
        mViewPager.offscreenPageLimit = fragmentDatas.size

        ll_logis_empty.visibility = View.GONE
        tv_logis_sum.text = "${fragmentDatas.size}"
        tv_logis_index.text = "1"


    }

    override fun initEvent() {
        mViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(p0: Int) {
            }

            override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
            }

            override fun onPageSelected(p0: Int) {
                tv_logis_index.text = "${p0 + 1}"
            }

        })
        iv_logis_close.setOnClickListener {
            dismiss()
        }
    }

    override fun startObserver() {

        mViewModel.getLogisItemListResultLiveData.observe(this, Observer {
            getLogisItemListResult(it)
        })
        mViewModel.getExpressList2ResultLiveData.observe(this, Observer {
            getExpressList2Result(it)
        })
    }


    override fun initData() {
        mViewModel.getExpressList2(orderDetailModel)

    }


    override val heightPercent: Double
        get() = 0.8
    override val gravity: Int
        get() = Gravity.CENTER
     fun getLogisItemListResult(data: ArrayList<LogisItemBean>) {

    }

     fun getExpressList2Result(guoneiBeanList: ArrayList<GuoneiBean>) {
        if (!guoneiBeanList.isNullOrEmpty()) {
            fragmentDatas.clear()
            fragmentDatas.addAll(guoneiBeanList)
            notifyViewPager()
        }
    }
}